package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.IServeProviderService;
import com.jzo2o.customer.service.ServeProviderCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class ServeProviderCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements ServeProviderCertificationAuditService {
    @Resource
    private WorkerCertificationMapper workerCertificationMapper;

    @Resource
    private IServeProviderService serveProviderService;

    /**
     * 服务人员提交认证
     *
     * @param dto 服务人员认证申请请求体
     */
    @Override
    public void workerCertificationAudit(WorkerCertificationAuditAddReqDTO dto) {
        //认证流程
        //健壮性，校验一下身份证
        String idCardNo = dto.getIdCardNo();
        if (!IdcardUtil.isValidCard(idCardNo)) {
            throw new BadRequestException("身份证号码不合法");
        }
        //1.将个人信息添加到认证信息表
        WorkerCertification workerCertification = BeanUtil.toBeanIgnoreError(dto, WorkerCertification.class);
        workerCertification.setCertificationTime(LocalDateTime.now());
        //查一下个人信息存不存在
        Long serveProviderId = UserContext.currentUserId();
        LambdaQueryWrapper<WorkerCertification> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkerCertification::getId, serveProviderId);
        WorkerCertification workerExist = workerCertificationMapper.selectOne(wrapper);

        workerCertification.setId(serveProviderId);

        if (workerExist == null) {
            //不存在就新增
            //新增时要修改状态为认证中
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationMapper.insert(workerCertification);
        } else {
            //如果认证失败，会再次进入
            //需要修改之前不合规的认证信息
            workerCertification.setCertificationTime(null);
            workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
            workerCertificationMapper.updateById(workerCertification);
        }

        //2.添加一条服务人员的认证申请
        WorkerCertificationAudit workerCertificationAudit = BeanUtil.toBean(dto, WorkerCertificationAudit.class);
        //前端传的无服务人员id，是在新增后才有id
        workerCertificationAudit.setServeProviderId(serveProviderId);

        this.save(workerCertificationAudit);
    }

    @Override
    public RejectReasonResDTO getRejectReason() {
        //从threadlocal获取服务人员id
        Long serveProviderId = UserContext.currentUserId();

        LambdaQueryWrapper<WorkerCertificationAudit> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkerCertificationAudit::getServeProviderId, serveProviderId)

                .orderByDesc(WorkerCertificationAudit::getUpdateTime)
                .last("limit 1");


        //查询认证记录
        WorkerCertificationAudit certificationAudit = getOne(wrapper);

        return new RejectReasonResDTO(certificationAudit.getRejectReason());
    }
}
